package com.xiewz;

import com.xiewz.executor.ImgMoveExecutor;
import com.xiewz.executor.WeiBoExecutor;

import java.io.File;

public class RunHandler {

    private String sourcePath;

    public void setSourcePath(String sourcePath) {
        this.sourcePath = sourcePath;
    }

    public RunHandler(String sourcePath) {
        this.sourcePath = sourcePath;
    }

    public boolean checkPath() {
        if (!sourcePath.endsWith(".md")) {
            return false;
        }
        File temp = new File(sourcePath);
        return temp.exists() && temp.isFile();
    }


    /**
     * 批量替换内容中的特殊字符
     */
    public String replaceChar() {
        String destFileName = "D:\\1lagoustudy\\workspace\\doc\\spring\\test2.md";
        destFileName = sourcePath;
        Trasfer.loadByFile(sourcePath);

        System.out.println(Trasfer.getContent());

        // 核心替换
        replace();
        String result = "替换目标文字个数: " + Trasfer.getRecordMap();
        System.out.println(result);

        Trasfer.saveToFile(destFileName);
        return result;
    }

    /**
     * 替换内容
     */
    private void replace() {
        Trasfer.replaceStr("⽀", "支");
        Trasfer.replaceStr("⽬", "目");
        Trasfer.replaceStr("⽅", "方");
        Trasfer.replaceStr("⽣", "生");
        Trasfer.replaceStr("⾼", "高");
        Trasfer.replaceStr("⾏", "行");
        Trasfer.replaceStr("⻆", "角");
        Trasfer.replaceStr("⾮", "非");
        Trasfer.replaceStr("⼦", "子");
        Trasfer.replaceStr("⼯", "工");
        Trasfer.replaceStr("⼊", "入");
        Trasfer.replaceStr("⾯", "面");
        Trasfer.replaceStr("⽤", "用");
        Trasfer.replaceStr("⼆", "二");
        Trasfer.replaceStr("⼀", "一");
        Trasfer.replaceStr("⽂", "文");
        Trasfer.replaceStr("⼼", "心");
        Trasfer.replaceStr("⼚", "厂");
        Trasfer.replaceStr("⾥", "里");
        Trasfer.replaceStr("⽆", "无");
        Trasfer.replaceStr("⽽", "而");
        Trasfer.replaceStr("⽐", "比");
        Trasfer.replaceStr("⻅", "见");
        Trasfer.replaceStr("⽇", "日");
        Trasfer.replaceStr("⼝", "口");
        // 更新
        Trasfer.replaceStr("⼿", "手");
        Trasfer.replaceStr("⾃", "自");
        Trasfer.replaceStr("⼏", "几");
        Trasfer.replaceStr("⼰", "己");
        Trasfer.replaceStr("⻄", "西");
        Trasfer.replaceStr("⻚", "页");
        Trasfer.replaceStr("⼗", "十");
        Trasfer.replaceStr("⾄", "至");
        Trasfer.replaceStr("⼋", "八");
        Trasfer.replaceStr("⻛", "风");
        Trasfer.replaceStr("⽚", "片");
        Trasfer.replaceStr("⼴", "广");
        Trasfer.replaceStr("⽹", "网");
        Trasfer.replaceStr("⾸", "首");
        Trasfer.replaceStr("⽌", "止");
        Trasfer.replaceStr("⽗", "父");
    }

    /**
     * 图片移动到文档路径下的img目录下，并替换为相对路径的图片
     */
    public void moveImgAndReplace(Boolean newFile) {
        // 目标文件路径
        String destFileName = newFile ? sourcePath.replace(".md", "_temp.md") : sourcePath;
        Trasfer.loadByFile(sourcePath);

        // 1. 移动图片到img下，替换图片路径为相对路径
        Trasfer.replaceImgPath(new ImgMoveExecutor(sourcePath));

        // 保存
        Trasfer.saveToFile(destFileName);
    }

    /**
     * 上传到微博图床，并替换内容中图片链接为微博图片链接
     */
    public void upluadWeiboAndReplace(Boolean newFile) {
        // 目标文件路径
        String destFileName = newFile ? sourcePath.replace(".md", "_temp.md") : sourcePath;
        Trasfer.loadByFile(sourcePath);

        // 2. 替换图片为微博床图地址
        Trasfer.replaceImgPath(new WeiBoExecutor(sourcePath));

        Trasfer.saveToFile(destFileName);
    }
}
